﻿Public Class frmProjectAndFile


    ''' <summary>
    ''' 新建工程
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnNewProject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewProject.Click
        If txtProject.Text.Length > 0 Then
            Dim sPrj As String = txtProject.Text
            For Each sExitPrj As String In lstPrjFl.Items
                If sExitPrj = sPrj Then
                    txtProject.BackColor = Color.Red
                    MsgBox("已有同名工程！")
                    Exit Sub
                End If
            Next

            txtProject.BackColor = Color.White
            PathAndFile.CreateProject(sPrj)

        End If

        SetSaveFileText()
    End Sub

    ''' <summary>
    ''' 工程名称成为焦点，列出已有工程
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtProject_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtProject.GotFocus
        DisplayExistProject()
    End Sub

    ''' <summary>
    ''' 显示已有工程
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub DisplayExistProject()
        'lstPrjFl.Top = txtProject.Bottom
        'lstPrjFl.Height = Me.Height - InputPanel1.Bounds.Height - lstPrjFl.Top
        'lstPrjFl.Visible = True

        Dim sprj() As String = PathAndFile.GetCreatedProjects.Split(",")
        lstPrjFl.Items.Clear()
        lstPrjFl.Items.Add("已有工程：")
        For Each prj As String In sprj
            If prj.Trim.Length > 0 Then lstPrjFl.Items.Add(prj.Trim)
        Next
    End Sub


    ''' <summary>
    ''' 选择已有工程或已有文件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub lstPrjFl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPrjFl.SelectedIndexChanged
        If lstPrjFl.Items(0) = "已有工程：" Then
            txtProject.BackColor = Color.White
            txtProject.Text = lstPrjFl.SelectedItem
            DisplayExistFile()

        ElseIf lstPrjFl.Items(0) = "已有文件：" Then
            txtCompareFile.Text = lstPrjFl.SelectedItem
            'lstPrjFl.Visible = False
            SetSaveFileText()
        End If

    End Sub

    ''' <summary>
    ''' 比对文件获得焦点，列出已有文件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtCompareFile_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCompareFile.GotFocus
        DisplayExistFile()
    End Sub
    Private Sub DisplayExistFile()
        'lstPrjFl.Top = txtCompareFile.Bottom
        'lstPrjFl.Height = Me.Height - InputPanel1.Bounds.Height - lstPrjFl.Top

        Dim pf As New PathAndFile(txtProject.Text)
        Dim sfile() As String = PathAndFile.GetExistFiles.Trim.Split(",")
        If sfile.Length = 1 And sfile(0).Length < 2 Then
            txtCompareFile.Text = ""
            SetSaveFileText()
            ' lstPrjFl.Visible = False

        Else
            Array.Sort(sfile)
            lstPrjFl.Items.Clear()
            lstPrjFl.Items.Add("已有文件：")
            For Each mfile As String In sfile
                lstPrjFl.Items.Add(mfile)
            Next
            If lstPrjFl.Items.Count > 3 Then lstPrjFl.TopIndex = lstPrjFl.Items.Count - 4
            ' lstPrjFl.Visible = True
        End If
    End Sub

    ''' <summary>
    ''' 根据比对文件，设置保存文件的文件名
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetSaveFileText()
        'lstPrjFl.Visible = False

        Dim sTime As Integer = 0
        If txtCompareFile.Text.Length > 0 Then
            Dim sLastTurn As String = txtCompareFile.Text
            Dim pos As Integer = InStr(sLastTurn, "-")
            If pos > 0 Then
                sTime = CInt(Mid(sLastTurn, pos + 1)) + 1
            End If
        End If
        txtSaveTo.Text = Today.ToString("yyyyMMdd") & "-" & String.Format("{0:D3}", sTime)
        txtSaveTo.Focus()
        txtSaveTo.SelectionStart = txtSaveTo.Text.Length

    End Sub

    ''' <summary>
    ''' 初始化显示
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub frmProjectAndFile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InputPanel1.Enabled = True
        DisplayExistProject()
    End Sub

    ''' <summary>
    ''' 返回前保存
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim MyReg As New GCS_Tools.GCS_Registry
        Dim sPrj As String = txtProject.Text.Trim
        Dim sSaveTo As String = txtSaveTo.Text.Trim

        If sPrj.Length < 1 Then
            txtProject.Focus()
            txtProject.BackColor = Color.Red
        Else
            MyReg.SetValue("沉降测量", "当前工程", txtProject.Text)
        End If

        If sSaveTo.Length < 1 Then
            txtSaveTo.Focus()
            txtSaveTo.BackColor = Color.Red
        Else
            MyReg.SetValue("沉降测量", "当前沉降数据保存到", txtSaveTo.Text)
        End If

        MyReg.SetValue("沉降测量", "当前比对文件", txtCompareFile.Text)

        Me.Close()
    End Sub


End Class